public class WebLoginActionTest extends TestCase {

    private WebLoginAction wla; //Your custom WW2 action that extends ActionSupport
    // If the WW2 action contains other complex objects (i.e. domain model objects), setUp() can be used to initialize those objects
    protected void setUp() throws Exception {
        wla = new WebLoginAction();
        wla.setJ_username("");
        wla.setJ_password(null);
        super.setUp();
    }

    // The WiKi shows different code for testing the validation for an action, the one below is proven to work
    public void testWebLoginActionValidation() throws ValidationException {
        ActionValidatorManager avm = ActionValidatorManagerFactory.getInstance();
        avm.validate(wla,"");
        Map fieldErrors = wla.getFieldErrors();

        assertTrue(wla.hasErrors());
        assertEquals(2, fieldErrors.size());
        assertTrue(fieldErrors.containsKey("j_username"));
        assertTrue(fieldErrors.containsKey("j_password"));

        System.out.println("[errors] : " +  fieldErrors.toString()); // Displays validated fields and its associated error messages
    }
}

 

 

Contributor: Carlos Cajina